//
//  SEGOrderTrackCell.m
//  Uhomebk
//
//  Created by mac on 2017/7/6.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "SEGOrderTrackCell.h"
#import "SEGOrderTrackView.h"


@interface SEGOrderTrackCell()<SEGOrderTrackViewDelegate>
{
    CGFloat trackBgViewHeight;/**轨迹视图高度 */
}
/**
 *  以下为自定义输入框所需成员变量 ---start
 */
@property (nonatomic, strong) UIView* bgView;/**背景 */

@property (nonatomic, strong) UIButton* sureBtn;/**确定按钮 */

@property (nonatomic, assign) CGFloat keyBoardY;/**键盘y轴*/

@property (nonatomic, assign) CGFloat textHei;/**文字高度 */

@property (nonatomic, assign) CGFloat inPutHeight;/**输入框高度 */

@property (nonatomic, strong) UIImageView* bgImageView;/**输入框背景图 */
/**
 *       --- end
 */
@property(nonatomic,strong)SEGOrderTrackView *currentTrackView;/**轨迹视图 */

@property(nonatomic,assign)SEGInputViewType inputType;

@property(nonatomic,strong )NSArray *dataSource;

@end

@implementation SEGOrderTrackCell

- (void)awakeFromNib {
    [super awakeFromNib];

    _lineView.backgroundColor = kColor_L2;
    [_titleLabel setTextColor:kColor_1];
    self.titleBackView.backgroundColor = kColor_W;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

/**
 填充数据
 @param data
 */
-(void)paddingDataWithMod:(id)data
{
    [self clearTrackBgView];
    SEGOrderDetailModel *mod = data;
    _dataSource = mod.trackInfos;
    [self setupTrackUIWith:mod.trackInfos];
}

-(void)clearTrackBgView
{
    NSArray *subArr = [self.trackBgView subviews];
    [subArr enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
}


/**
 布局
 */
- (void)setupTrackUIWith:(NSArray *)dataArray
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = kColor_W;
    
    //检测工单是否有被加急过，有被加急过隐藏加急按钮
    [self checkOrderIsUrgentWith:dataArray];
    
    _currentTrackView = nil;
    trackBgViewHeight = 0;
    BOOL __block isFirstTrack = YES;
    [dataArray enumerateObjectsUsingBlock:^(SEGOrderTrackInfoModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        //系统回访的轨迹去掉
        BOOL isSystemCallBack = [model.trackCode isEqualToString:@"NODE_DS_RECALL"] && [model.opUserType isEqualToString:@"2"];
        // 审核环节轨迹去掉
        BOOL isExamineTrack = [model.trackCode isEqualToString:@"NODE_DS_CHECK"];
        if (!isSystemCallBack && !isExamineTrack) {
            SEGOrderTrackView *trackView = [self.trackBgView viewWithTag:999+idx];
            if (trackView == nil) {
                trackView = [[SEGOrderTrackView alloc] initWithFrame:CGRectMake(0, trackBgViewHeight, LYScreenWidth(), 0) trackModel:model parentVC:self.parentVC isFirstTrack:isFirstTrack];
                trackView.tag = 999+idx;
                trackView.delegate = self;
                trackView.parentVC = self.parentVC;
                [self.trackBgView addSubview:trackView];
                _trackBgView.backgroundColor = kColor_W;
            }
            else{
                [trackView refreshSubViewsWith:model];
            }
            [self fitUIWith:trackView];
            trackBgViewHeight += trackView.trackViewHeight;
            isFirstTrack = NO;
        }
    }];
    [self fitContentView];
}


/**
 展示用户名展示回调   - 刷新UI
 */
-(void)openUserNameRefreshUI
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(openUserNameRefreshTableView)]) {
        [self.delegate openUserNameRefreshTableView];
    }
}

/**
 刷新界面
 */
-(void)refreshUI:(NSArray *)dataArray
{
    //检测工单是否有被加急过，有被加急过隐藏加急按钮
    [self checkOrderIsUrgentWith:dataArray];
    
    _currentTrackView = nil;
    trackBgViewHeight = 0;
    [dataArray enumerateObjectsUsingBlock:^(SEGOrderTrackInfoModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        if(idx == 0){
            model.isLast = @"1";
        } else {
            model.isLast = @"0";
        }
        SEGOrderTrackView *trackView = [self.trackBgView viewWithTag:999+idx];
        if (trackView == nil) {
            trackView = [[SEGOrderTrackView alloc] initWithFrame:CGRectMake(0, trackBgViewHeight, LYScreenWidth(), 0) trackModel:model parentVC:self.parentVC isFirstTrack:idx == 0];
            trackView.tag = 999+idx;
            trackView.delegate = self;
            trackView.parentVC = self.parentVC;
            [self.trackBgView addSubview:trackView];
        }
        _trackBgView.backgroundColor = kColor_W;
        [trackView refreshSubViewsWith:model];
        [self fitUIWith:trackView];
        trackBgViewHeight += trackView.trackViewHeight;
        
    }];
    [self fitContentView];
}

-(void)fitUIWith:(SEGOrderTrackView *)trackView
{
    [trackView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.right.equalTo(self.trackBgView);
        make.top.equalTo(self.trackBgView.mas_top).offset(trackBgViewHeight);
        make.size.mas_equalTo(CGSizeMake(LYScreenWidth(), trackView.trackViewHeight));
        NSLog(@"trackViewHeight = %f",trackView.trackViewHeight);
    }];
   
}

-(void)fitContentView{
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        // iOS10 以上的适配
        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
            make.left.right.top.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(LYScreenWidth(), trackBgViewHeight+44));
        }else{
            make.edges.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(LYScreenWidth(), trackBgViewHeight+44));
//            NSLog(@"trackBgViewHeight = %f",trackBgViewHeight);
        }
    }];
}

-(void)fitUIWithMoreTrack:(UIView *)trackView
{
    
    trackBgViewHeight += CGRectGetMaxY(self.currentTrackView.frame) ;
    __block CGFloat nowHeight = trackBgViewHeight;
    NSLog(@"currentTrackBgView = %@",NSStringFromCGRect(self.currentTrackView.frame));
    NSLog(@"contentView = %@",NSStringFromCGRect(self.contentView.frame));
    NSLog(@"trackBgView = %@",NSStringFromCGRect(self.trackBgView.frame));
    [trackView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.trackBgView);
        make.top.equalTo(self.trackBgView).offset(nowHeight);
        make.size.mas_equalTo(CGSizeMake(LYScreenWidth(), trackView.frame.size.height));
        
    }];
    
    [self.trackBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(LYScreenWidth(), nowHeight+trackView.height));
    }];
    
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        // iOS10 以上的适配
        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
            make.left.right.top.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(LYScreenWidth(), nowHeight+trackView.height+44));
        }else{
            make.edges.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(LYScreenWidth(), nowHeight+trackView.height+44));
        }
    }];
    NSLog(@"trackView = %@",NSStringFromCGRect(trackView.frame));
    NSLog(@"contentView = %@",NSStringFromCGRect(self.contentView.frame));
    NSLog(@"trackBgView = %@",NSStringFromCGRect(self.trackBgView.frame));
}


/**
 检测工单是否有被加急过，有被加急过隐藏加急按钮

 @param dataArray
 */
-(void)checkOrderIsUrgentWith:(NSArray *)dataArray
{
    if (dataArray.count == 0) {
        return;
    }
    __block BOOL isUrgent = NO;
    [dataArray enumerateObjectsUsingBlock:^(SEGOrderTrackInfoModel *trackModel, NSUInteger idx, BOOL * _Nonnull stop) {
        [trackModel.tasks enumerateObjectsUsingBlock:^(SEGOrderTasks * taskModel, NSUInteger idx, BOOL * _Nonnull stop) {
            if (taskModel.urgentinfo && taskModel.urgentinfo.discussion.length > 0) {
                isUrgent = YES;
                *stop = YES;
            }
        }];
        if (isUrgent) {
            *stop = YES;
        }
    }];
}

@end
